package reflectdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectClassDemo {
    /**
     * 反射获取对象
     */
    public static void reflectNewInstance(){
        Class<?> c3=null;
        try {
            c3=Class.forName("reflectdemo.Student");//获取一个Class对象方法3
            try {
                Student student=(Student)c3.newInstance();
                System.out.println(student);
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 通过反射 调用构造方法 【公开和私有的都可以】
     */
    public static void reflectPrivateConstructor(){
        Class<?> c3=null;
        try {
            c3=Class.forName("reflectdemo.Student");//获取一个Class对象方法3
            Constructor<?> constructor= c3.getDeclaredConstructor(String.class,int.class);//得到构造方法
            constructor.setAccessible(true);//获得私有要加这名话
            Student student=(Student) constructor.newInstance("laowang",20);
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     *反射私有属性
     */
    public static void reflectPrivateField() {
        Class<?> c3=null;
        try {
            c3=Class.forName("reflectdemo.Student");//获取一个Class对象方法3
           Student student=(Student)c3.newInstance();
          Field field= c3.getDeclaredField("name");
          field.setAccessible(true);
          field.set(student,"zhangfei");
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 反射私有方法
     */
    public static void reflectPrivateMethod() {
        Class<?> c3=null;
        try {
            c3=Class.forName("reflectdemo.Student");//获取一个Class对象方法3
            Student student=(Student)c3.newInstance();
            Method method=c3.getDeclaredMethod("function", String.class);
            method.setAccessible(true);
            method.invoke(student,"一个参数");


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }

    }

    public static void main(String[] args) {
       // reflectNewInstance();
       // reflectPrivateConstructor();
       // reflectPrivateField();
        reflectPrivateMethod();

    }
}
